var TemplateReportWidget = (function(){
	var currentFilter;
	var actions = {
			templates_feed:'',
			preview_feed:'',
			content_save:''
	};

	return {
		init : function(feed_url, preview_url, contentsave_url){
			var that = this;
			currentFilter = $j('input:radio[name=template-filter]:checked').val();
			actions.templates_feed = feed_url;
			actions.preview_feed = preview_url;
			actions.content_save = contentsave_url;
			
			$j("#AddPreview").click(function(){
				that.addPreviewToContent($j("#preview").text());
			});
			$j('input[type="radio"]').click(function(){
				//si el filtro seleccionado es igual al actual no hacer nada
				if (currentFilter == this.value) return;
				currentFilter = this.value;
				that.getTemplates(currentFilter);
			});

			$j("a#TemplateDialog").fancybox({
				title:"Nueva Plantilla",
				onCleanup : function(){
					that.getTemplates(currentFilter);
				}
			});
			
			$j("input#NewTemplate").click(function(){
				that.addTemplate();
				return false;
			});
			
			this.attachTemplateEvent();
			
			$j("input#btnSaveContent").click(function(){
				var report_content = $j("textarea#content").val(); 
				console.log(report_content);
				that.saveContent(report_content);
				return false;
			});
					
		},
		destroy : function(){
		},
		attachTemplateEvent : function(){
			var that = this;
			$j("select#templates option").click(function(){
				that.getTemplatePreview(this.value);
			});
		},
		addPreviewToContent : function(template){
			var content = $j("#content");
			var current = content.val();
			current += (current.length > 0)? "\n":"";
			current += template;
			content.val(current);
		},
		addTemplate : function(){
			//como fancybox solo funciona con links lo que hago es disparar el evento click al tocar
			//el input button
			$j("a#TemplateDialog").trigger("click");
		},
		getTemplates : function(selected_filter){
			var that = this;
			$j.post(actions.templates_feed,{filter : selected_filter},function(response){
				that.updateTemplateList(eval('('+ response + ')'));
			});
			currentFilter = selected_filter;
		},
		getTemplatePreview : function(template_id){
			var that = this;
			$j.post(actions.preview_feed,{id:template_id},function(response){
				response = eval('(' + response + ')');
				that.updatePreview(response.template);
			});
		},
		saveContent : function(report_content){
			if (report_content.length > 0){
				$j.post(actions.content_save,{content:report_content},function(response){
					console.log(response);
					return false;
				});
				return;
			}
		},
		updatePreview : function(templateText){
			$j("#preview").text(templateText);
		},
		updateTemplateList : function(template_list){
			var templates = $j("#templates"); //el combo multiple
			var DomTemplates = document.createDocumentFragment();
							
			for (var i=0;i < template_list.length; i++){
				var optionElem = $j("<option></option").attr("value",template_list[i].id).html(template_list[i].value).get(0);
				DomTemplates.appendChild(optionElem);
			}

			//Flushing section
			templates.html("");
			this.updatePreview("- No se ha seleccionado ninguna plantilla -");
								
			templates.append(DomTemplates); //cargo la nueva template list
			this.attachTemplateEvent();	//llamo nuevamente al metodo attachTemplateEvent		
		}
	};
}());